package com.demo.util;

import java.io.*;

/**
 * @program: homework
 * @author: Mr.Zhuo
 * @create: 2022-04-12 14:45
 **/
public class FileUtil {


    // 读取文件
    public static String readTxt(String path) throws Exception {
        StringBuilder strb = new StringBuilder("");

        // 获取文件流
        InputStream is = new FileInputStream(new File(path));

        InputStreamReader isr = new InputStreamReader(is, getCode(path));

        BufferedReader br = new BufferedReader(isr);

        String str = "";

        while (null != (str = br.readLine())) {
            strb.append(str);

            strb.append(":");
        }

        br.close();

        return strb.toString();

    }

    // 获取编码格式 gb2312,UTF-16,UTF-8,Unicode,UTF-8
    public static String getCode(String path) throws Exception {
        InputStream inputStream = new FileInputStream(path);

        byte[] head = new byte[3];

        inputStream.read(head);

        String code = "UTF-8"; // 或GBK

        if (head[0] == -1 && head[1] == -2)

            code = "UTF-8";

        else if (head[0] == -2 && head[1] == -1)

            code = "UTF-8";

        else if (head[0] == -17 && head[1] == -69 && head[2] == -65)

            code = "UTF-8";

        inputStream.close();

        return code;

    }
}
